/*
 * Copyright (c) 2001, 2002 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.perforce.core.internal.api;

import java.util.ArrayList;
import java.util.List;

import net.sourceforge.perforce.api.IPerforceCommand;
import net.sourceforge.perforce.core.PerforceCorePlugin;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;

/**
  * ClientUser object for the 'p4 depots' command.
  * 
  * @version $Revision: 129 $
  * @author  <a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  * 
  * TODO: Introduce IDepotsResult
  */
public class Depots extends AbstractPerforceCommand implements IPerforceCommand
{
	// --------------------------------------------------------------------------- Private Variables
	
	/** Stores the result of the operation. */
	private List result = new ArrayList();
	
	// --------------------------------------------------------------- Public Static Factory Methods
	
	/** Convenience method for creating instances of Depots.
	  * 
	  * @param monitor for progress feedback.
	  * @return the new Depots instance.
	  */
	public static final Depots newInstance(IProgressMonitor monitor)
	{
		MultiStatus status = new MultiStatus(PerforceCorePlugin.ID, IStatus.OK, "", null);
		return new Depots(status, monitor);
	}

	// ------------------------------------------------------------------------ Private Constructors
	
	/** Creates a new instance. 
	  * 
	  * @param status the MultiStatus.
	  * @param monitor for progress feedback.
	  */
	private Depots(MultiStatus status, IProgressMonitor monitor)
	{
		super(status, monitor);
	}
	
	// -------------------------------------------------------- Public Methods (IClientUserDelegate)
	
	/** @see IClientUserDelegate#outputInfo(char, infoString) */
	public void outputInfo(char level, String infoString)	
	{		
		if (infoString.startsWith("Depot "))
		{
			int start = infoString.indexOf(' ') + 1;
			int stop  = infoString.indexOf(' ', start);
								
			result.add("//" + infoString.substring(start, stop));                      //$NON-NLS-1$
			
			getProgressMonitor().subTask("//" + infoString.substring(start, stop));    //$NON-NLS-1$
			getProgressMonitor().worked(1);
		}
		else
		{
			IStatus tempStatus = PerforceStatus.decode(infoString, IStatus.INFO);
			getMultiStatus().merge(tempStatus);
		}
	}
	
	// ----------------------------------------------------------- Public Methods (IPerforceCommand)

	/** @see IPerforceCommand#getCommand()  */
	public String getCommand()
	{
		return "depots";                                                               //$NON-NLS-1$
	}

	/** @see IPerforceCommand#getStatus()  */
	public IStatus getStatus()
	{
		return getMultiStatus();
	}
	
	/** @see IPerforceCommand#isHidingFromConsole() */
	public boolean isHidingFromConsole()
	{
		return false;
	}	
	// ------------------------------------------------------------------------------ Public Methods
	
	/** Returns the result of the operation.
	  * 
	  * @return array of depot names
	  */
	public String[] getResult()	
	{
		// When the perforce admin didn't create a depot explicitly, perforce will have on named
		// 'depot' implicitly. Unfortunately, this isn't returned by the 'depots' command, so
		// we just fake the result.
		if (result.size() == 0)
		{
			return new String[] { "//depot" };                                         //$NON-NLS-1$
		}
		else
		{
			return (String[]) result.toArray(new String[result.size()]);
		}
	}
}
